package gui_View;

import java.util.ArrayList;

import models.Listvid;
import adaoters.ListvidAdapter;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import clicks.Listvideoclick;
import clicks.OnLoadMoreListener;
import example.demo_navi.R;

public class Playlistview extends ListView implements android.widget.AdapterView.OnItemClickListener, OnScrollListener{

	private ArrayList<Listvid> al;
	private Listvideoclick lvc;
	private OnLoadMoreListener mOnLoadMoreListener;
	private OnScrollListener mOnScrollListener;
	private LayoutInflater mInflater;
	private RelativeLayout mFooterView;
	private ProgressBar mProgressBarLoadMore;
	private boolean mIsLoadingMore = false;
	private int mCurrentScrollState;
	private static final String TAG = "PlayListView";
	
	public Playlistview(Context ct,AttributeSet as,int df){
		super(ct, as, df);
		init(ct);
	}
	public Playlistview(Context ct,AttributeSet as){
		super(ct, as);
		init(ct);
	}
	public Playlistview(Context ct){
		super(ct);
		init(ct);
	}
	
	public void setPlaylist(ArrayList<Listvid> al){
		this.al=al;
		ListvidAdapter lva=new ListvidAdapter(getContext(), al);
		setAdapter(lva);
		setOnItemClickListener(this);
	//	lva.notifyDataSetChanged();
	}
	
	public void setonPlistvi(Listvideoclick l){
		this.lvc=l;
	}
	
	@Override
	public void setAdapter(ListAdapter adapter) {
		// TODO Auto-generated method stub
		super.setAdapter(adapter);
	}
	
	public void onItemClick(AdapterView<?> ada, View v, int pos, long id) {
		// TODO Auto-generated method stub
		if(lvc!=null){
			lvc.onListvidClicked(al.get(pos));
		}
	}
	
	private void init(Context context) {

		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	
		mFooterView = (RelativeLayout) mInflater.inflate(
				R.layout.load_more_footer, this, false);
	
		mProgressBarLoadMore = (ProgressBar) mFooterView
				.findViewById(R.id.load_more_progressBar);

		addFooterView(mFooterView);

		super.setOnScrollListener(this);
	}
	
	@Override
	public void setOnScrollListener(OnScrollListener l) {
		// TODO Auto-generated method stub
		mOnScrollListener=l;
	}
	
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (mOnScrollListener != null) {
			mOnScrollListener.onScroll(view, firstVisibleItem,
					visibleItemCount, totalItemCount);
		}

		if (mOnLoadMoreListener != null) {

			if (visibleItemCount == totalItemCount) {
				mProgressBarLoadMore.setVisibility(View.GONE);
				return;
			}

			boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

			if (!mIsLoadingMore && loadMore
					&& mCurrentScrollState != SCROLL_STATE_IDLE) {
				mProgressBarLoadMore.setVisibility(View.VISIBLE);
				mIsLoadingMore = true;
				onLoadMore();
			}

		}

	}
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		mCurrentScrollState = scrollState;

		if (mOnScrollListener != null) {
			mOnScrollListener.onScrollStateChanged(view, scrollState);
		}
	}
	
	public void onLoadMore() {
		Log.d(TAG, "onLoadMore");
		if (mOnLoadMoreListener != null) {
			mOnLoadMoreListener.onLoadMore();
		}
	}
	
	public void onLoadMoreComplete() {
		mIsLoadingMore = false;
		mProgressBarLoadMore.setVisibility(View.GONE);
	}
	
	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}
}
